<?php

namespace app\models;

use think\Model;
use EasyWeChat\Factory;


class Order extends Model
{
    protected $pk = 'id';
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'created';
    protected $updateTime = 'updated';

    public function getStatusTextAttr($value, $data)
    {
        $text = '';
        switch ($data['status']) {
            case '1':
                $text = '待审核';
                break;
            case '2':
                $text = '待出库';
                break;
            case '3':
                $text = '待发货';
                break;
            case '4':
                $text = '待收货';
                break;
            case '5':
                $text = '已收货';
                break;
            case '6':
                $text = '已取消';
                break;
            default:
                # code...
                break;
        }
        if (!empty($data['shouhou']) && $data['shouhou'] == 2 && $data['status'] >= 1 && $data['status'] <= 4) {
            $text = $text . '[售后中]';
        }
        return $text;
    }


    public function getReviewTextAttr($value, $data)
    {
        $text = '';
        switch ($data['review']) {
            case '1':
                $text = '待付款';
                break;
            case '2':
                $text = '待确认';
                break;
            case '3':
                $text = '已付款';
                break;
            default:
                # code...
                break;
        }
        return $text;
    }

    //通知提醒
    public function Message($openid = '', $template_id = '', $data = [], $appid, $pagepath)
    {
        $config = config('wechat2.');
        $app = Factory::officialAccount($config);
        $send_data = [
            'template_id' => $template_id, // 所需下发的订阅模板id
            'touser' => $openid,     // 接收者（用户）的 openid
            'miniprogram' => [
                'appid' => $appid,
                'pagepath' => $pagepath,
            ],
            'data' => $data
        ];
        $result = $app->template_message->send($send_data);
        return $result;
    }
}
